<template>
  <div class="song-list">
    <van-nav-bar
      :title="singerName"
      left-text=""
      right="icon"
      left-arrow
      :flex="false"
      @click-left="onClickLeft"
    />
    <van-image round width="10rem" height="10rem" :src="singerPic" />
    <div class="music-list">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.id"
          :title="item.name"
          @click="toPlay(item.id)"
        />
      </van-list>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      singerPic: "",
      singerName: "",
    };
  },
  created() {
    // 获取歌手图片和名字
    this.singerPic = localStorage.getItem("singerPic");
    this.singerName = localStorage.getItem("singerName");
    // 获取歌手id以及其歌曲
    axios
      .get(
        "http://localhost:3000/artist/songs?id=" +
          localStorage.getItem("singerId")
      )
      .then((res) => {
        this.list = res.data.songs;
        /* console.log(res.data.songs); // 作者所有歌曲
        console.log(res.data.songs[0].name); // 作者第一首歌的名称
        console.log(res.data.songs[0].id); // 作者第一首歌的id */
      });
  },
  methods: {
    toPlay(id) {
      localStorage.setItem("singleId", id);
      this.$router.push({
        name: "Play",
      });
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 10) {
          this.finished = true;
        }
      }, 1000);
    },
  },
};
</script>

<style>
.jianjie-head {
  width: 100vw;
  height: 25vh;
  background: paleturquoise;
  position: relative;
}
.jianjie-head img {
  width: 38vw;
  height: 20vh;
  float: left;
  margin-top: 3vh;
}
.jianjie-head h4 {
  position: absolute;
  top: 25%;
  left: 45%;
}
.music-list {
  width: 100vw;

  border-radius: 10px 10px 0 0;
}
.music-list h4 {
  width: 90vw;
  height: 8vh;
  border-bottom: #eee 1px solid;
  margin: 0 auto;
  line-height: 8vh;
}
.van-nav-bar .van-icon {
  color: red !important;
}
.song-list .van-image {
  margin: 2vw auto;
  display: block;
}
</style>
